<!--
 * Copyright © 2022
 * @Author: zhangxiang
 * @Date: 2022-11-21 17:13:52
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-02-02 10:34:42
 * @Description:
 *
-->

<template>
  <MyContainer class="container">
    <MyHeader class="header">
      <el-input
        v-model="searchKey"
        style="width: 200px; margin-right: 20px"
        size="medium"
        class="input"
        placeholder="请输入关键字进行过滤"
        clearable
        @clear="clearClick"
        @keyup.enter.native="queryClick"
      />
      <el-button
        size="medium"
        type="primary"
        @click="queryClick"
      >查询</el-button>
      <el-button size="medium" @click="resetClick">重置</el-button>
    </MyHeader>
    <MyMain class="main">
      <el-table
        v-loading="loading"
        class="common-table"
        :data="list"
        stripe
        header-cell-class-name="listHeaderCell"
        :header-cell-style="{ color: '#696969' }"
      >
        <el-table-column
          prop="cloudAccountName"
          label="云账号名称"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          prop="cloudType"
          label="云类型"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          prop="cloudProvider"
          label="云服务商"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          prop="accountStatus"
          label="账号状态"
          min-width="120"
        />
        <el-table-column prop="syncStatus" label="同步状态" min-width="120" />
        <el-table-column prop="syncDate" label="上次同步时间" width="170" />
        <el-table-column prop="createBy" label="创建人" min-width="120" />
        <el-table-column prop="createDate" label="创建时间" width="170" />
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              style="height: 22px; padding: 0 10px"
              :loading="scope.row.syncLoading"
              @click="syncClick(scope.row, scope.$index)"
            >同步</el-button>
            <el-popover
              :ref="`popover-delete-${scope.$index}`"
              placement="top"
              width="300"
              style="margin-left: 10px"
            >
              <p>是否确认删除？</p>
              <div style="text-align: right; margin: 0">
                <el-button
                  size="mini"
                  type="text"
                  @click="
                    scope._self.$refs[
                      `popover-delete-${scope.$index}`
                    ].doClose()
                  "
                >取消</el-button>
                <el-button
                  type="primary"
                  size="mini"
                  @click="deleteClick(scope)"
                >确定</el-button>
              </div>
              <el-button
                slot="reference"
                type="danger"
                style="height: 22px; padding: 0 10px"
                size="mini"
              >删除</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" style="margin-top: 30px">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next"
          :page-sizes="[10, 15, 20]"
          :page-size="pageSize"
          :current-page.sync="pageNum"
          :total="totalCount"
          @current-change="changePage"
          @size-change="sizeChange"
        />
      </el-row>
    </MyMain>
  </MyContainer>
</template>

<script>
import pagePermissionMixin from '@/views/mixin/pagePermission.js'
import pageQueryMixin from '@/views/mixin/pageQuery.js'
import MyContainer from '@/components/Layout/MyContainer.vue'
import MyHeader from '@/components/Layout/MyHeader.vue'
import MyMain from '@/components/Layout/MyMain.vue'
import { getCloudList, deleteCloud, syncCloud } from '@/api/cn-mgmt'

export default {
  name: 'CNList',
  components: {
    MyContainer,
    MyHeader,
    MyMain
  },
  mixins: [pagePermissionMixin, pageQueryMixin],
  data() {
    return {}
  },
  created() {},
  methods: {
    /**
     * @description: 同步
     * @param {*} row
     * @return {*}
     */
    syncClick(row, index) {
      this.$set(this.list[index], 'syncLoading', true)
      syncCloud({
        cloudAccountName: row.cloudAccountName
      }).then((res) => {
        this.$message.success('同步成功')
        this.getList() // 请求数据
      }).finally(() => {
        this.list[index].syncLoading = false
      })
    },
    /**
     * @description: 删除
     * @param {*} row
     * @param {*} index
     * @return {*}
     */
    deleteClick(scope) {
      const row = scope.row
      deleteCloud({
        cloudAccountName: row.cloudAccountName
      }).then((res) => {
        scope._self.$refs[`popover-delete-${scope.$index}`].doClose() // 取消popover
        this.$message.success('删除成功')
        if (this.list.length === 1) {
          // 当前页只有一条数据时 删除完刷新上一页数据
          this.pageNum = Math.max(this.pageNum - 1, 1)
        }
        this.getList() // 请求数据
      })
    },
    /**
     * @description: 获取算网列表
     * @return {*}
     */
    getList() {
      this.handleRequestAndResp(getCloudList, {
        keyword: this.searchKey,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    }
  }
}
</script>

<style lang="less" scoped>
@import "@/assets/css/common.less";
.container {
}
</style>
